package com.boarsoft.boar.office.group.biz;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.office.entity.OfficeGroup;
import com.boarsoft.boar.office.group.OfficeGroupBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("officeGroupBiz")
public class OfficeGroupBizImpl extends SimpleBizImpl implements OfficeGroupBiz {

	@Override
	@Transactional(readOnly = true)
	public PagedResult<OfficeGroup> list(String key, int pageNo, int pageSize, String orderBy) {
		StringBuffer sb = new StringBuffer();
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(OfficeGroup.class, sb.toString());
		if (Util.strIsNotEmpty(orderBy)) {
			sb.append(" order by ").append(orderBy).append(" desc");
		} else {
			sb.append(" order by name");
		}
		List<OfficeGroup> alt = dao.list(OfficeGroup.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<OfficeGroup>(total, alt, pageNo, pageSize);
	}

	@Override
	@Transactional
	public boolean add(OfficeGroup o) {
		dao.save(o);
		return true;
	}

	@Override
	@Transactional
	public boolean update(OfficeGroup o) {
		return dao.update(o);
	}

	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(OfficeGroup.class, id);
	}

	@Override
	@Transactional(readOnly = true)
	public OfficeGroup get(String id) {
		return (OfficeGroup) dao.get(OfficeGroup.class, id);
	}
}